﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DeepZoomPivotConstructor.DTBuilderLib;

namespace DeepZoomSLUtilities.HotSpots.Specific
{
    /// <summary>
    /// When the user clicks in this HS we attempt to center the page (and zoom in a bit if we've not already).
    /// </summary>
    public class HSCenter : HotSpotActionBase
    {
        Rect CenterOnThis;

        /// <summary>
        /// Grab data from the meta data. If the data is null, then we will
        /// just center on our hotspot. Otherwise, move to whatever rect has been
        /// specified.
        /// </summary>
        /// <param name="data"></param>
        public HSCenter(HotSpotInfo parent, DZHotSpotMetaData.ActionData data)
            :base (parent, data)
        {
            if (data.LinkData == null)
            {
                CenterOnThis = HSInfo.Boundary;
            }
            else
            {
                string[] coords = data.LinkData.Split(',');
                if (coords.Length != 4)
                {
                    CenterOnThis = HSInfo.Boundary;
                }
                else
                {
                    double x = Convert.ToDouble(coords[0]);
                    double y = Convert.ToDouble(coords[1]);
                    double w = Convert.ToDouble(coords[2]);
                    double h = Convert.ToDouble(coords[3]);
                    CenterOnThis = new Rect(x, y, w, h);
                }
            }
        }

        /// <summary>
        /// Center the thing.
        /// </summary>
        internal override void Execute()
        {
            Rect global = HSInfo.SubImage.ConvertLocalToSubImage(CenterOnThis);
            HotSpotInfo.ZoomControl.CenterRectVisible(global);
        }
    }
}
